﻿using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.SystemFacade;
using Fuse8.DomainFramework.Common.Locator;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Fuse8.ContentManagementFramework.Web.ApplicationRuntime
{
    public static class FileManager
    {
        public static void Remove(this IEnumerable<CmsMediaFile> mediaFiles)
        {
            if (mediaFiles == null || !mediaFiles.Any()) return;
            var currentContext = DependencyServiceLocator.Current.GetInstance<ICmsContext>();

            foreach (var cmsMediaFile in mediaFiles)
            {
                var pathToFile = Path.Combine(currentContext.MapPath(CmsConfiguration.AssetUploadPath), cmsMediaFile.LinkedFileName);
                if (File.Exists(pathToFile))
                {
                    try
                    {
                        File.Delete(pathToFile);
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }
            }
        }

        public static void Remove(this CmsMediaFile mediaFile)
        {
            Remove(new List<CmsMediaFile> { mediaFile });
        }
    }
}